#include	"interrupt.h"
#include	"can.h"
#include	"init.h"

uint16_t	F_Time_1ms    = 0;
uint16_t	F_Time_2ms    = 0;
uint16_t	F_Time_10ms   = 0;
uint16_t	F_Time_33ms   = 0;
uint16_t	F_Time_50ms   = 0;
uint16_t	F_Time_400ms  = 0;
uint16_t	F_Time_500ms  = 0;
uint16_t	F_Time_100ms  = 0;
uint16_t	F_Time_1000ms = 0;
uint16_t	F_Time_5000ms = 0;
uint16_t	F_Time_1Min   = 0;

uint16_t	T_2MS_Cnt    = 2;
uint16_t	T_10MS_Cnt   = 10;
uint16_t	T_33MS_Cnt   = 33;
uint16_t	T_50MS_Cnt   = 5;
uint16_t	T_100MS_Cnt  = 10;
uint16_t	T_400MS_Cnt  = 40;
uint16_t	T_500MS_Cnt  = 50;
uint16_t	T_1000MS_Cnt = 100;
uint16_t	T_5000MS_Cnt = 500;
uint16_t	T_1Min_Cnt   = 60;

uint16_t	C_Time_100ms = 0;
uint16_t	C_Time_500ms = 0;
uint16_t	C_Time_1000ms = 0;

uint16_t	C_100MS_Cnt = 100;
uint16_t	C_500MS_Cnt = 500;
uint16_t	C_1000MS_Cnt = 1000;

uint16_t A_Time_100ms  = 0;
uint16_t A_Time_1000ms = 0;

uint16_t A_100MS_Cnt  = 100;
uint16_t A_1000MS_Cnt = 1000;

//************************************************************************************
//	Interrupt routine Multi
//************************************************************************************
void TimeInit(void)
{
	F_Time_1ms    = 0;
	F_Time_2ms    = 0;
	F_Time_10ms   = 0;
	F_Time_33ms   = 0;
	F_Time_50ms   = 0;
	F_Time_100ms  = 0;
	F_Time_400ms  = 0;
	F_Time_500ms  = 0;
	F_Time_1000ms = 0;
	F_Time_1Min   = 0;

	T_2MS_Cnt    = 2;
	T_10MS_Cnt   = 10;
	T_33MS_Cnt   = 33;
	T_50MS_Cnt   = 5;
	T_100MS_Cnt  = 10;
	T_400MS_Cnt  = 40;
	T_500MS_Cnt  = 50;
	T_1000MS_Cnt = 100;
	T_1Min_Cnt   = 60;

	C_Time_100ms = 0;
	C_Time_500ms = 0;
	C_Time_1000ms = 0;
	
	C_100MS_Cnt = 5;
	C_500MS_Cnt = 5;
	C_1000MS_Cnt = 5;
	
	A_Time_100ms  = 0;
	A_Time_1000ms = 0;
	
	A_100MS_Cnt  = 5;
	A_1000MS_Cnt = 5;
}
void ISR_TM2(void)
{
	if((Controller.L_KeepWakeUp == 0)&&(Controller.E_Wakeup == 0))
	{
		C_Time_100ms = 0;
		C_Time_500ms = 0;
		C_Time_1000ms = 0;

		C_100MS_Cnt = 5;
		C_500MS_Cnt = 5;
		C_1000MS_Cnt = 5;
		
		A_Time_100ms  = 0;
		A_Time_1000ms = 0;
		
		A_100MS_Cnt  = 5;
		A_1000MS_Cnt = 5;
	}
	
	A_100MS_Cnt--;
	if(A_100MS_Cnt == 0) 			
	{
		A_100MS_Cnt = 100;
		A_Time_100ms = 1;       //100ms flag
	}
	
	A_1000MS_Cnt--;
	if(A_1000MS_Cnt == 0)
	{
		A_1000MS_Cnt = 1000;
		A_Time_1000ms = 1;		 //1s flag
	}

	C_100MS_Cnt--;
	if(C_100MS_Cnt == 0)
	{
		C_100MS_Cnt = 100;
		C_Time_100ms = 1;		 //100ms flag
	}
	
	C_500MS_Cnt--;
	if(C_500MS_Cnt == 0)
	{
		C_500MS_Cnt = 500;
		C_Time_500ms = 1;		 //500ms flag
	}

	C_1000MS_Cnt--;
	if(C_1000MS_Cnt == 0)
	{
		C_1000MS_Cnt = 1000;
		C_Time_1000ms = 1;		 //1s flag
	}

	
	F_Time_1ms = 1;							//1ms flag
	
	T_2MS_Cnt--;
	if(T_2MS_Cnt == 0)
	{
		T_2MS_Cnt = 2;
		F_Time_2ms = 1;
	}
	
	T_33MS_Cnt--;
	if(T_33MS_Cnt == 0) 			//20ms
	{
		T_33MS_Cnt = 33;
		F_Time_33ms = 1;
	}
	T_10MS_Cnt--;
	if(T_10MS_Cnt == 0)
	{
		T_10MS_Cnt = 10;
		F_Time_10ms = 1;					//10ms flag
		
		if(CAN1_AircdRxResetTime1)
			CAN1_AircdRxResetTime1--;
		if(CAN1_AircdRxResetTime2)
			CAN1_AircdRxResetTime2--;
		if(CAN1_AircdRxResetTime3)
			CAN1_AircdRxResetTime3--;
		if(CAN2_AircdRxResetTime1)
			CAN2_AircdRxResetTime1--;
		
		T_50MS_Cnt--;
		if(T_50MS_Cnt == 0) 			//50ms
		{
			T_50MS_Cnt = 5;
			F_Time_50ms = 1;
		}
		T_100MS_Cnt--;
		if(T_100MS_Cnt == 0) 			//100ms
		{
			T_100MS_Cnt = 10;
			F_Time_100ms = 1;
		}
		T_400MS_Cnt--;
		if(T_400MS_Cnt == 0)
		{
			T_400MS_Cnt = 40;
			F_Time_400ms = 1;				//400ms flag
		}
		T_500MS_Cnt--;
		if(T_500MS_Cnt == 0)
		{
			T_500MS_Cnt = 50;
			F_Time_500ms = 1;				//500ms flag
		}
		T_1000MS_Cnt--;
		if(T_1000MS_Cnt == 0)
		{
			T_1000MS_Cnt = 100;
			F_Time_1000ms = 1;		 //1s flag
			
			T_1Min_Cnt--;
			if(T_1Min_Cnt == 0)
			{
				T_1Min_Cnt = 60;
				F_Time_1Min = 1;		 //60s flag
			}
		}
		T_5000MS_Cnt--;
		if(T_5000MS_Cnt == 0)
		{
			T_5000MS_Cnt = 500;
			F_Time_5000ms = 1;		 //5s flag
		}
	}
}


